package com.serviceprovider.business.utility;

//~--- non-JDK imports --------------------------------------------------------

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

//~--- JDK imports ------------------------------------------------------------

import java.io.IOException;
import java.io.InputStream;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {
    static Properties  props  = null;
    private static Log logger = LogFactory.getLog(SendMail.class);
    static boolean     loaded = false;

    static {
        props = new Properties();

        InputStream inputStream = SendMail.class.getResourceAsStream("/email.properties");

        try {
            props.load(inputStream);
        } catch (IOException e) {
            logger.error("Exception reading properties: " + e.getMessage(), e);
        }

        loaded = true;
    }

    String bccAddresses   = "";
    String fileAttachment = "";
    String from           = "";
    String host           = "";
    String message        = "";
    String subject        = "";
    String to             = "";

    public void send() throws Exception {
        logger.debug("sending email");

        if (loaded) {
            if ("".equals(to)) {
                to = props.getProperty("email.from");
            }

            // Get session
            Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(props.getProperty("email.username"),
                                                      props.getProperty("email.password"));
                }
            });

            session.setDebug(true);

            // Define message
            MimeMessage message = new MimeMessage(session);

            message.setFrom(new InternetAddress(from));

            InternetAddress[] replyToAddresses = { new InternetAddress(from) };

            message.setReplyTo(replyToAddresses);
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(this.subject);
            message.setHeader("content-type", "text/html");

            // Put parts in messag
            message.setContent(this.message, "text/html");
            Transport.send(message);

            //
            // StringTokenizer tokenizer = new
            // StringTokenizer(bccAddresses,",");
            // while(tokenizer.hasMoreTokens()){
            // message.setRecipients(Message.RecipientType.TO,
            // tokenizer.nextToken());
            // Transport.send( message );
            //
            // }
        } else {
            logger.error("Email properties not loaded");
        }
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getBccAddresses() {
        return bccAddresses;
    }

    public void setBccAddresses(String bccAddresses) {
        this.bccAddresses = bccAddresses;
    }
}



